// global variables / objects
var processingInProgressPopup, ajax;
var formInputFields;
$(function() {
    // jquery defaults
    $.datepicker.setDefaults({showOn: 'button', buttonImage: 'images/calendar.gif', buttonImageOnly: true});

    processingInProgressPopup = {
        popupElementName : '#processing-order-popup',
        showPopup : function(){
            $(processingInProgressPopup.popupElementName).css({ "top" : ( $(window).height() - $(processingInProgressPopup.popupElementName).height() ) / 2+$(window).scrollTop() + "px"});
            $(processingInProgressPopup.popupElementName).css({ "left" : ( $(window).width() - $(processingInProgressPopup.popupElementName).width() ) / 2+$(window).scrollLeft() + "px"});
            $(processingInProgressPopup.popupElementName).show();
        },
        hidePopup : function(){
            $(processingInProgressPopup.popupElementName).hide();
        }
    }

    ajax = {
        defaultContentType : 'application/x-www-form-urlencoded; charset=cp1251',
        makeUrl : function(aUrl, aParams, aExtraParams){
            var count = 0;
            for (var aParamName in aParams) {
                aUrl += ((count == 0) ? "?" : "&") + aParamName + "=" + encodeURI(aParams[aParamName]);
                count++;
            }
            if(aExtraParams != null){
                for (var aExtraParamName in aExtraParams) {
                    aUrl += "&" + aExtraParamName + "=" + encodeURI(aExtraParams[aExtraParamName]);
                }
            }
            return aUrl;
        },
        runSyncAjaxCall : function(aUrl,
                                   aParams,
                                   aExtraParams,
                                   onSuccessCallback,
                                   enableProcessingPopup){
            $.ajax({
                async : false,
                url : ajax.makeUrl(aUrl, aParams, aExtraParams),
                contentType : ajax.defaultContentType,
                success : onSuccessCallback,
                beforeSend: function(){
                    if(enableProcessingPopup){
                        processingInProgressPopup.showPopup();
                    }
                },
                complete: function(){
                    if(enableProcessingPopup){
                        processingInProgressPopup.hidePopup();
                    }
                }
            })
        }
    }

    formInputFields = {
        inputFiledIdStartsWith : 'input-',
        getAllInputFieldsValues : function(parentElementId){
			var formDataText = 'var formData = {';
			$('#' + parentElementId + ' input[id^=' + formInputFields.inputFiledIdStartsWith + ']').each(function(index, item){
					var itemNativeId = $(item).attr('id').substring(formInputFields.inputFiledIdStartsWith.length);
					formDataText += (index == 0 ? '' : ',') + '\'' + itemNativeId + '\'' + ' : ' + ($(item).val() ? '\'' + $(item).val() + '\'' : '\'\'') ;
			});
			formDataText += '}';
			eval(formDataText);
			return formData;
        }
    }

});

